const express = require('express');
const router = express.Router()
const pool = require('../utils/pool.js')
const Response = require('../utils/res')
const $ =require('../utils/method')
//我的分店
router.get('/fws-shop',(req,res,next)=>{
  const sql = 'select SQL_CALC_FOUND_ROWS shop.*,sum(salestotal) zxl,sum(estimate) hp from shop,sdetails where sid=shid and shop_ident like ? group by shop limit ?,?;select FOUND_ROWS() total'
  const {ident,page} = req.query
  pool.query(sql,[ident+'-%',req.start,req.pages],(err,r)=>{
    if(err){
      return next(err)
    }
    if(r[0].length){
      res.send({
        code:200,
        msg:'ok',
        data:r[0],
        page:page-1||1,
        pagesize:req.pages,
        total:r[1][0].total
      })
    }else{
      res.send(Response.error(406,'未查询到任何数据'))
    }
  })
})
//我的门店
router.get('/fws-meShop',(req,res,next)=>{
  const sql = 'select SQL_CALC_FOUND_ROWS shop.* from shop join merchant on `zh_id` = mid where shop_ident=?;select FOUND_ROWS() total'
  const {ident,page}=req.query
  pool.query(sql,[ident,req.strat,req.pages],(err,r)=>{
    if(err){
      return next(err)
    }
    if(r[0].length){
      res.send({
        code:200,
        msg:'ok',
        data:r[0],
        page:page-1||1,
        pagesize:req.pages,
        total:r[1][0].total
      })
    }else{
      res.send(Response.error(406,'未查询到任何数据'))
    }
  })
})

//按门店id查询服务详情
router.get('/server-ListByshop',(req,res,next)=>{
  const {page,sid}=req.query
  const sql = 'select sdetails.*,shop,shop.shop,shop.shop_avatar,shop.longit,shop.latit from shop,sdetails where sid=shid and sid=? limit ?,?;select count(*) total from sdetails where shid=?'
  pool.query(sql,[sid,req.start,req.pages,sid],(err,r)=>{
    if(err){
      return next(err)
    }
    res.send({
      code:200,
      msg:'ok',
      data:$.CombineData(r[0],['shid','shop','shop_avatar','longit','latit']),
      total:r[1][0].total,
      page:page-0||1,
      pagesize:req.pages})
  })
})


module.exports = router